package com.dragon.utilslibrary.tools;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.text.TextUtils;

import java.util.List;
import java.util.Stack;

/**
 * Activit堆栈统一管理类
 */

public class RunActivityManager {

    private static RunActivityManager instance;
    private static Stack<Activity> activityStack;

    public synchronized static RunActivityManager getInstance() {
        if (instance == null) {
            instance = new RunActivityManager();
        }
        return instance;
    }

    /**
     * 判断该activity是否在显示
     *
     * @param className
     * @return
     */
    public boolean isTopActivity(Context context, String className) {
        if (TextUtils.isEmpty(className)) {
            return false;
        }
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 关掉栈顶activity
     */
    public void finish() {
        Activity activity = activityStack.pop();
        if (activity != null) {
            activity.finish();
            activity = null;
        }
    }

    /**
     * 关闭某个activity
     *
     * @param activity
     */
    public void finish(Activity activity) {
        if (activity != null) {
            activity.finish();
            activityStack.remove(activity);
            activity = null;
        }
    }

    public boolean removeActivity(Class<?> cls) {
        int size = activityStack.size();
        for (int i = 0; i < size; i++) {
            Activity activity = activityStack.get(i);
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                return activityStack.remove(activity);
            }
        }
        return false;
    }

    /**
     * 关掉某个activity，
     *
     * @param cls
     */
    public void finish(Class<?> cls) {
        if (activityStack != null) {
            int size = activityStack.size();
            for (int i = 0; i < size; i++) {
                Activity activity = activityStack.get(i);
                if (activity == null) {
                    break;
                }
                if (activity.getClass().equals(cls)) {
                    finish(activity);
                    break;
                }
            }

        }
    }

    /**
     * 取出盏顶的activity
     *
     * @return
     */
    public Activity currentActivity() {
        Activity activity = null;
        try {
            if (activityStack != null && activityStack.size() > 0) {
                activity = activityStack.lastElement();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return activity;
    }

    /**
     * 堆栈activity
     *
     * @param activity
     */
    public void pushActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(activity);
    }

    public void remove(Activity activity) {
        if (null != activityStack) {
            activityStack.remove(activity);
        }
    }

    /**
     * 关掉所有activity除了cls
     *
     * @param cls
     */
    public void finishAllActivityExceptOne(Class<?> cls) {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                Activity activity1 = currentActivity();
                if (activity1 != null) {
                    finish(activity1);
                } else {
                    break;
                }
            }
            finish(activity);
        }
    }

    /**
     * 关掉所有activity除了cls
     *
     * @param cls
     */
    public void finishAllActivityExceptOne2(Class<?> cls) {
        if (activityStack != null)
            for (Activity activity : activityStack) {
                if (!activity.getClass().equals(cls)) {
                    activity.finish();
                }
            }
    }

    /**
     * 关闭所有activity
     */
    public void finishAllActivity() {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            finish(activity);
        }
    }

}
